package com.itheima.tanhuan.vo.mongo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.itheima.tanhuan.pojo.mongo.UserLocation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserLocationVo implements java.io.Serializable{

    private static final long serialVersionUID = 4508868382007529970L;

    @Id
    private ObjectId id;
    @Indexed
    private Long userId; //用户id
    private String longitude; //x:经度
    private String latitude;// y:纬度
    private String address; //位置描述
    private Long created; //创建时间
    private Long updated; //更新时间
    private Long lastUpdated; //上次更新时间

    public static UserLocationVo getUserLocationVo(UserLocation userLocation) {
        UserLocationVo userLocationVo =new UserLocationVo();
        BeanUtil.copyProperties(userLocation,userLocationVo);
        GeoJsonPoint location = userLocation.getLocation();
        List<Double> coordinates = location.getCoordinates();
        userLocationVo.setLongitude(Convert.toStr(coordinates.get(0)));
        userLocationVo.setLatitude(Convert.toStr(coordinates.get(1)));
        return userLocationVo;
    }
}
